/*
 * 23/10/2011 - 19:04
 */
package org.nettyflux.udp;

import java.net.InetAddress;
import org.nettyflux.communicator.RemoteHost;

/**
 * @author Victor Williams Stafusa da Silva
 */
public final class UDPHost implements RemoteHost {

    private final InetAddress address;
    private final int localPort;
    private final int remotePort;

    public UDPHost(InetAddress address, int localPort, int remotePort) {
        if (address == null || localPort < 0 || localPort > 0xFFFF || remotePort < 0 || remotePort > 0xFFFF) {
            throw new IllegalArgumentException();
        }
        this.address = address;
        this.localPort = localPort;
        this.remotePort = remotePort;
    }

    public InetAddress getAddress() {
        return address;
    }

    public int getLocalPort() {
        return localPort;
    }

    public int getRemotePort() {
        return remotePort;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) return true;
        if (obj == null || this.getClass() != obj.getClass()) return false;
        UDPHost other = (UDPHost) obj;
        return this.address.equals(other.address)
                && this.localPort == other.localPort
                && this.remotePort == other.remotePort;
    }

    @Override
    public int hashCode() {
        return this.address.hashCode() + this.localPort + (this.remotePort << 16);
    }

    @Override
    public String toString() {
        return "Address: " + this.address
                + ", UDP local port: " + this.localPort
                + ", UDP remote port: " + this.remotePort;
    }
}
